package net.yla.smartbus.module.controller.web.ba.employee;

import net.yla.smartbus.core.api.ApiResult;
import net.yla.smartbus.core.base.BaseController;
import net.yla.smartbus.core.constants.UserRoleEnum;
import net.yla.smartbus.module.ba.dept.service.DeptService;
import net.yla.smartbus.module.ba.employee.entity.EmployeeEntity;
import net.yla.smartbus.module.ba.employee.query.EmployeeQuery;
import net.yla.smartbus.module.ba.employee.service.EmployeeService;
import net.yla.smartbus.module.ba.right.roles.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description:
 * @author: Yee
 * @date: 2021/6/28 13:52
 */
@RestController
@RequestMapping("/api/web/employee")
public class EmployeeController extends BaseController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    DeptService deptService;
    @Autowired
    RolesService rolesService;

    /**
     * 查询符合条件员工的记录集
     *
     * @param query
     * @return
     */
    @PostMapping("/query")
    public ApiResult findEmp(@RequestBody EmployeeQuery query) {
//        rolesService.userRoleEvaluation(UserRoleEnum.PERSON_SELECT, getToken(), getIpAddress());
        return success(employeeService.findEmp(query));
    }


    /**
     * 获取部门下拉框
     *
     * @return
     */
    @PostMapping("/departments")
    public ApiResult departments() {
        String token = getToken();
        return success(deptService.departments(token));
    }

    /**
     * 逻辑删除人员
     *
     * @param employeeQuery
     * @return
     */
    @PostMapping("delPerson")
    public ApiResult delPerson(@RequestBody EmployeeQuery employeeQuery) {
        rolesService.userRoleEvaluation(UserRoleEnum.PERSON_DELETE, getToken(), getIpAddress());
        return success(employeeService.delPersons(employeeQuery.getPersonIds()));
    }

    /**
     * 编辑人员信息
     *
     * @param employeeDto
     * @return
     */
    @PostMapping("/editPerson")
    public ApiResult findEmp(HttpServletRequest request, @RequestBody EmployeeEntity employeeDto) {
        if (employeeDto.getEmpId() == null) {
            rolesService.userRoleEvaluation(UserRoleEnum.PERSON_INSERT, getToken(), getIpAddress());
        } else {
            rolesService.userRoleEvaluation(UserRoleEnum.PERSON_UPDATE, getToken(), getIpAddress());
        }

        return success(employeeService.editPerson(request, employeeDto));
    }


    /**
     * 人员信息导出
     *
     * @param response
     * @param query
     */
    @PostMapping("/export/emp")
    public void exportListByQuery(HttpServletResponse response, EmployeeQuery query) {
        employeeService.exportEmployees(response, query);
    }


    /**
     * 导出人员信息模板
     *
     * @param response
     */
    @PostMapping("/export/template")
    public void exportPersonTemplate(HttpServletResponse response) {
        employeeService.exportPersonTemplate(response);
    }


    /**
     * 导入人员信息
     *
     * @param request
     * @param file
     * @throws IOException
     */
    @PostMapping("/import/person")
    public void uploadPerson(HttpServletRequest request, MultipartFile file) throws IOException {
        employeeService.uploadPerson(request, file);
    }

}
